<?php

namespace NewAdmin\Controller;
use Think\Controller;
class CrowdfundingController extends CommonController {
  
    public function index(){
        $crowdfunding=M('crowdfunding');
        if ($_POST) {

        }else{
            $userArray  = $this->$userArray;
            $user=$userArray['userArray'];
            $uid=$user['id'];
            $crowdfundinglist=$crowdfunding->where("c_type=0 and uid=$uid")->select();
        }
        $shop   =   M('shop');
        foreach ($crowdfundinglist as $key=>$val){
            $goods_id=$val["goods_id"];
            $shop_info=$shop->field('yt_shop.id,yt_shop.title')->where("id=$goods_id")->find();
            $crowdfundinglist[$key]['product_name']=$shop_info['title'];
        }
        //print_r($crowdfundinglist);
        $this->assign('crowdfundinglist',$crowdfundinglist);
        $this->display();

    }
    public function area_list(){
        $crowdfunding=M('crowdfunding');
        $crowdfundinglist=$crowdfunding->where("c_type=1")->select();

        $shop   =   M('shop');
        foreach ($crowdfundinglist as $key=>$val){
            $goods_id=$val["goods_id"];
            $shop_info=$shop->field('yt_shop.id,yt_shop.title')->where("id=$goods_id")->find();
            $crowdfundinglist[$key]['product_name']=$shop_info['title'];
        }
        $this->assign('crowdfundinglist',$crowdfundinglist);
        $this->assign('title','区域众筹管理');
        $this->display();

    }

    public function edit_area_staus(){
        $crowdfunding=M('crowdfunding');
        $data['id']=I('post.id');
        $data['c_status']=1;
        $rs=$crowdfunding->save($data);
        if($rs){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }
    
    public function add(){
    	if($_POST){
             $userArray  = $this->$userArray;
             $user=$userArray['userArray'];
  	 		 $uid=$user['id'];
    		 $data=I('post.');
             $crowdfunding=M('crowdfunding');
             $shop   =   M('shop');
             //开启事物
             $crowdfunding->startTrans();
             $shop->startTrans();
    		 $add_info['uid']=$uid;
    		 $add_info['project_name']=$data['project_name'];
    		 $add_info['total_num']=$data['total_num'];
    		 $add_info['total_money']=$data['total_money'];
             $add_info['days']=$data['days'];
    		 $add_info['start_time']=$data['start_time'];
    		 $add_info['end_time']=$data['end_time'];
    		 $add_info['create_time']=date('Y-m-d H:i:s',time());
    		 $add_info['update_time']=date('Y-m-d H:i:s',time());
    		 if($data['start_time']<=$add_info['create_time']){
                 $add_info['state']=1;
             }
             //print_r($data['ids']);
             $ids=explode(',',$data['ids']);
             foreach($ids as $key=>$value){
                $add_info['goods_id']=$value;
                $shop_list=$shop->where(array('id'=>$value,'is_crowdfunding'=>'1'))->find();
                if($shop_list){
                     echo "0|||该商品已参加众筹，请重新选择商品";
                     die;
                }
             }


             $crowdfunding_id=$crowdfunding->add($add_info);
            if($crowdfunding_id){
                $shop->where('id='.$add_info['goods_id'])->setField(array('is_crowdfunding'=>'1','crowdfunding_id'=>$crowdfunding_id));
                $crowdfunding->commit();
                $shop->commit();
                echo "0|||添加成功|||/Crowdfunding/index/";
            }else{
                $crowdfunding->rollback();
                $shop->rollback();
                echo "0|||添加失败|||/Crowdfunding/add/";
            }
    	}else{
    		 $this->display();
    	}
      
    }

		function edit(){
			$id=I('get.id');
            $userArray  = $this->$userArray;
            $user=$userArray['userArray'];
            $uid=$user['id'];
            $crowdfunding=M('crowdfunding');
            $crowdfunding_info=$crowdfunding->where("uid=$uid and id=$id")->find();
            //print_r($crowdfunding_info);
            $shop   =   M('shop');
            $goods_id=$crowdfunding_info["goods_id"];

            $shop_info=$shop->field('yt_shop.id,yt_shop.title,yt_shop.num,yt_shop.image,yt_shop.price')->where("id=$goods_id")->select();
            $ids[0]=$shop_info[0]["id"];
            $shop_info=json_encode($shop_info);
            $ids=json_encode($ids);
            //print_r($ids);print_r($shop_info);
            $this->assign('ids',$ids);

            $this->assign('shop_info',$shop_info);
            $this->assign('crowdfunding_info',$crowdfunding_info);
			$this->display();
		}

		function update(){
			if($_POST){
			    //print_r($_POST);
			 $userArray  = $this->$userArray;
             $user=$userArray['userArray'];
             $uid=$user['id'];
             $data=I('post.');
             $id=$data['id'];
                $crowdfunding=M('crowdfunding');
                $shop   =   M('shop');
                //开启事物
                $crowdfunding->startTrans();
                $shop->startTrans();
                $crowdfunding_info=$crowdfunding->where("uid=$uid and id=$id")->find();
    		    //已发放张数
                if($crowdfunding_info["num"]>$data['total_num']){
                    echo "0|||已被领取".$crowdfunding_info["num"]."张，更新总张数不能低于".$crowdfunding_info["num"]."张|||";
                    die;
                }
                if($crowdfunding_info["state"]!=='0'){
                    if($crowdfunding_info["start_time"]>$data['end_time']){
                        echo "0|||众筹活动已开始截止时间不能早于开始时间|||";
                        die;
                    }
                }
                $shop->where('id='.$crowdfunding_info['goods_id'])->setField(array('is_crowdfunding'=>'0','crowdfunding_id'=>null));
                $add_info['uid']=$uid;
                $add_info['project_name']=$data['project_name'];
                $add_info['total_num']=$data['total_num'];
                $add_info['total_money']=$data['total_money'];
                $add_info['days']=$data['days'];
                $add_info['start_time']=$data['start_time'];
                $add_info['end_time']=$data['end_time'];
                $add_info['create_time']=date('Y-m-d H:i:s',time());
                $add_info['update_time']=date('Y-m-d H:i:s',time());
                if($data['start_time']<=$add_info['create_time']){
                    $add_info['state']=1;
                }else{
                    $add_info['state']=0;
                }
                //print_r($data['ids']);
                $ids=explode(',',$data['ids']);
                foreach($ids as $key=>$value){
                    $add_info['goods_id']=$value;
                    $shop_list=$shop->where(array('id'=>$value,'is_crowdfunding'=>'1'))->find();
                    if($shop_list){
                        echo "0|||该商品已参加众筹，请重新选择商品";
                        die;
                    }
                    $shop->where('id='.$value)->setField(array('is_crowdfunding'=>'1','crowdfunding_id'=>$id));
                }

                $crowdfunding_id=$crowdfunding->where('id='.$id)->save($add_info);
                //print_r($crowdfunding_id);
                if($crowdfunding_id){
                    $crowdfunding->commit();
                    $shop->commit();
                    echo "0|||修改成功|||/Crowdfunding/index/";
                }else{
                    $crowdfunding->rollback();
                    $shop->rollback();
                    echo "0|||修改失败|||";
                }
    	}
		}

    // 删除众筹
    function delete(){
        if(isset($_GET['id'])){
            $crowdfunding=M('crowdfunding');
            $shop   =   M('shop');
            //开启事物
            $crowdfunding->startTrans();
            $shop->startTrans();
            $id = I("get.id",0,'intval');
            $crowdfunding_info=$crowdfunding->where("id=$id")->find();
            $shop->where('id='.$crowdfunding_info['goods_id'])->setField(array('is_crowdfunding'=>'0','crowdfunding_id'=>null));
            $crowdfunding_id=$crowdfunding->where('id='.$id)->delete();
            if ($crowdfunding_id) {
                $crowdfunding->commit();
                $shop->commit();
                $this->ajax_r ( 1,'删除成功 ',array('url' => U('Crowdfunding/index')));
            } else {
                $crowdfunding->rollback();
                $shop->rollback();
                $this->ajax_r ( 0,'删除失败 ');
            }
        }
    }
    /**
     * 获取众筹详情
     */
    public function getCrowdfundingInfo()
    {
        $coupon_type=M('coupon_type');
        $id = $_POST['id'];
        $crowdfunding=M('crowdfunding');
        $data=$crowdfunding->where("id=$id")->find();
        $shop   =   M('shop');
        $goods_id=$data["goods_id"];
        $shop_info=$shop->field('yt_shop.id,yt_shop.title')->where("id=$goods_id")->find();
        $data['product_name']=$shop_info['title'];

        $this->ajaxReturn($data);
     
    }


     public function getsearchgoodslist(){
        $shop   =   D('shop');
        $where ='ishidden=0 ';
        $title  =   preg_replace('/\//',' ',I('get.title'));
        if( $title ){
            $titleArray =   explode(' ',$title);
            if( $titleArray ){
                foreach ( $titleArray AS $key=>$row ){
                    $where  .=  $row != ''?' AND title LIKE "%'.$row.'%"':'';
                }
            }
        }
        $where  .=  I('get.id') !== ''?' AND id = '.I('get.id').'':'';
        $array  =   $shop->field("id,title,price,image,num")->where($where)->order('id DESC')->limit(8)->select();
				$data['data']=$array;
				$this->ajaxReturn($data);
     }
     
     public function Spellgroup(){
     	$shop=M('shop');
     	$storeArray  =   $this->storeArray;
  	 	$uid=$storeArray['id'];
     	$rs=$shop->where('ishidden=0 and tuanselect=1 and store_id='.$uid)->select();
     	$this->assign('rs',$rs);
     	
     	$this->display();
     }
     
     public function daiyan(){
     	$storeArray  =   $this->storeArray;
  	 	$uid=$storeArray['id'];
		$count_rs=M('order_goods')
     		->join('yt_users on yt_users.id=yt_order_goods.recommend_id')
     		->where("yt_order_goods.store_id=$uid and yt_order_goods.recommend_id<>''")
     		->group('yt_order_goods.recommend_id')
			->select();
     	$count=count($count_rs);
    	$Page = new \Think\Page ($count,15); // 实例化分页类 传入总记录数
		$Page->setConfig('header','条记录');
	    $Page->setConfig('prev','上一页');
	    $Page->setConfig('next','下一页');
		$show = $Page->show(); // 分页显示输出
		$rs=M('order_goods')
     		->join('yt_users on yt_users.id=yt_order_goods.recommend_id')
     		->where("yt_order_goods.store_id=$uid and yt_order_goods.recommend_id<>''")
     		->group('yt_order_goods.recommend_id')
     		->limit($Page->firstRow.','.$Page->listRows)
     		->select();
		$this->assign('page',$show);
        $this->assign('rs',$rs);
     	$this->display();
     }
	  public function daiyan_info(){
     	$id=I('get.id');
		$storeArray  =   $this->storeArray;
		$uid=$storeArray['id'];
		
		$user_info=M('users')->find($id);
		$this->assign('user_info',$user_info);
		//代言次数
		$daiyan_num=get_count_daiyan($id,$uid);
		$succse_daiyan_num=get_count_succse_daiyan($id,$uid);
     	//代言红包
		$daiyan_price=get_count_daiyan_prcie($id,$uid);
		//待入账
		$daiyan_dr_price=get_count_daiyan_dr_prcie($id,$uid);
	
		$this->assign('daiyan_num',$daiyan_num);
		$this->assign('succse_daiyan_num',$succse_daiyan_num);
		$this->assign('daiyan_price',$daiyan_price);
		$this->assign('daiyan_dr_price',$daiyan_dr_price);
		$count=M('order_goods')
			->join('yt_users on yt_users.id=yt_order_goods.recommend_id')
			->where("yt_order_goods.store_id=$uid and yt_order_goods.recommend_id=$id")
			->count();
		
		$Page = new \Think\Page ($count,15); // 实例化分页类 传入总记录数
		$Page->setConfig('header','条记录');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$show = $Page->show(); // 分页显示输出
		$rs=M('order_goods')
			->join('yt_order ON yt_order_goods.order_id = yt_order.order_id','left')
    		->field("yt_order.buyer_name,yt_order.order_sn,yt_order.add_time,yt_order.order_state,yt_order.refund_state,yt_order_goods.*")
    		->where("yt_order_goods.recommend_id=$id and yt_order_goods.store_id=$uid")
    		->limit($Page->firstRow.','.$Page->listRows)
    		->order('yt_order_goods.rec_id desc')
    		->select();
		$this->assign('page',$show);
		$this->assign('rs',$rs);
		$this->display();
     }
   

}
